﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Results;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;

namespace ZHDJ.AppInterface.Controllers
{
    public class ArticleController : ApiController
    {
        #region 获取文章
        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">条数</param>
        /// <param name="module"></param>
        /// <param name="category">分类</param>
        /// <param name="searchKey"></param>
        /// <param name="banchId"></param>
        /// <returns></returns>
        [HttpPost]
        public ReturnResult GetArticle(dynamic obj)
        {
            DataTable dt = new DataTable();
            IList<PartyArticle> list = new List<PartyArticle>();

            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string module = Convert.ToString(obj.module);
            string category = Convert.ToString(obj.category);
            string searchKey = Convert.ToString(obj.searchKey);
            string banchId = Convert.ToString(obj.banchId);

            if (string.IsNullOrWhiteSpace(module))
                module = "InformationNewsletter";

            dt = InterfaceHelper.GetPartyArticleGridData(pageIndex, pageSize, module, category, searchKey, banchId).Tables[0];
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyArticle>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = dt.Rows.Count.ToString()
            };
        }
        #endregion

        #region 获取文章实体
        /// <summary>
        /// 获取文章实体
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public ReturnResult GetArticleModel(dynamic obj)
        {
            string ID = Convert.ToString(obj.ID);
            string Module = Convert.ToString(obj.Module);
            PartyArticle model = new PartyArticle();
            if (Module == "JoinPublicity")
            {
                //发展党员
                PartyJoinPublicity p_model = InterfaceHelper.GetJoinpublicityModel(ID);
                model.ID = p_model.ID;
                model.Title = p_model.Title;
                model.CreateTime = p_model.CreateTime;
                model.ImageUrl = p_model.ImageUrl;
                model.Content = p_model.Title;
            }
            else if (Module == "RelationnshipPublicity")
            {
                //组织关系转接
                PartyRelationnshipPublicity p_model = InterfaceHelper.GetRelationnshipPublicityModel(ID);
                model.ID = p_model.ID;
                model.Title = p_model.Title;
                model.CreateTime = p_model.CreateTime;
                model.ImageUrl = p_model.ImageUrl;
                model.Content = p_model.Title;
            }
            else if (Module == "VolunteerActivitie")
            {
                //志愿活动召集
                PartyVolunteerActivitie p_model = InterfaceHelper.GetPartyVolunteerActivitieModel(ID);
                model.ID = p_model.ID;
                model.Title = p_model.Title;
                model.CreateTime = p_model.CreateTime;
                model.ImageUrl = p_model.ImageUrl;
                model.Content = InterfaceHelper.NoHTML(p_model.Summary);
            }
            else if (Module == "VolunteerQuotation")
            {
                //志愿活动心语
                PartyVolunteerQuotation p_model = InterfaceHelper.GetPartyVolunteerQuotationModel(ID);
                model.ID = p_model.ID;
                model.Title = p_model.Title;
                model.CreateTime = p_model.CreateTime;
                model.ImageUrl = p_model.ImageUrl;
                model.Content = InterfaceHelper.NoHTML(p_model.Content);
            }
            else
            {
                //文章
                model = InterfaceHelper.GetPartyArticleModel(ID);
            }

            return new ReturnResult()
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion

        #region 获取门户文章轮播图片
        /// <summary>
        /// 获取门户文章轮播图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ReturnResult GetPortalArticleImage()
        {
            DataTable dt = InterfaceHelper.GetPortalArticleImage();
            IList<PartyArticle> list = new List<PartyArticle>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyArticle>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 获取支部文章
        [HttpPost]
        public ReturnResult GetBranchArticle(dynamic obj)
        {
            DataTable dt = new DataTable();
            IList<PartyArticle> list = new List<PartyArticle>();
            ReturnResult result = new ReturnResult();
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string module = Convert.ToString(obj.module);
            string category = Convert.ToString(obj.category);
            string searchKey = Convert.ToString(obj.searchKey);
            string banchId = Convert.ToString(obj.banchId);

            if (module == "JoinPublicity")
            {
                //发展党员
                dt = InterfaceHelper.GetJoinpublicity(pageIndex, pageSize, banchId);
            }
            else if (module == "RelationnshipPublicity")
            {
                //组织关系转接
                dt = InterfaceHelper.GetRelationnshipPublicity(pageIndex, pageSize, banchId);
            }
            else if (module == "VolunteerActivitie")
            {
                //志愿活动召集
                dt = InterfaceHelper.GetVolunteerActivitie(pageIndex, pageSize, banchId);
            }
            else if (module == "VolunteerQuotation")
            {
                //志愿活动心语
                dt = InterfaceHelper.GetVolunteerQuotation(pageIndex, pageSize, banchId);
            }
            else
            {
                //文章
                dt = InterfaceHelper.GetThreeAffairOpenList(banchId, module, category, "limit " + (pageIndex) * pageSize + "," + pageSize + "");
            }

            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyArticle>.ConvertToModel(dt);

            result.code = "200";
            result.result = list;
            result.msg = "success";

            return result;
        }
        #endregion

        #region 添加文章
        [HttpPost]
        public ReturnResult Add(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            try
            {
                string[] array = Convert.ToString(obj.memberString).TrimEnd(',').Split(',');
                string MessageType = Convert.ToString(obj.MessageType);
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = Convert.ToString(obj.Title);
                article.Content = Convert.ToString(obj.content);
                article.Module = "Announcement";
                article.Category = "Basic";
                article.Status = 4;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = Convert.ToString(obj.CreateBy);
                article.ModifyBy = Convert.ToString(obj.CreateBy);
                InterfaceHelper.AddArticle(article);

                //批量添加文章阅读进度表
                InterfaceHelper.BatchAddArticleRead(article.ID, array, article.CreateBy);
                result.code = "200";
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.code = "201";
                result.msg = "保存失败！";
            }
            return result;
        }
        #endregion

        #region 添加文章
        [HttpPost]
        public ReturnResult AddArticle(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            PartyArticle article = new PartyArticle();
            article.ID = StringHelper.NewID;
            article.Title = Convert.ToString(obj.Title);
            article.Subhead = Convert.ToString(obj.Subhead);
            article.Author = Convert.ToString(obj.Author);
            article.AuthorUrl = Convert.ToString(obj.AuthorUrl);
            article.ArticlesLable = Convert.ToString(obj.ArticlesLable);
            article.Summary = Convert.ToString(obj.Summary);
            article.Flags = Convert.ToInt32(obj.Flags);
            article.BranchID = Convert.ToString(obj.BranchID);

            article.Content = Convert.ToString(obj.content);
            article.Module = Convert.ToString(obj.Module);
            article.Category = Convert.ToString(obj.Category);
            article.Status = 4;
            article.CreateTime = DateTime.Now;
            article.ModifyTime = DateTime.Now;
            article.CreateBy = Convert.ToString(obj.CreateBy);
            article.ModifyBy = Convert.ToString(obj.CreateBy);

            string ImageUrl = Convert.ToString(obj.ImageUrl);
            if (!string.IsNullOrWhiteSpace(ImageUrl))
            {
                PartyFile file = new PartyFile();
                file.ID = StringHelper.NewID;
                file.FID = "";
                file.Title = article.Title;
                file.Extension = ".jpg";
                file.Description = article.Title;
                file.Size = 0;
                file.Url = ImageUrl;
                file.CreateTime = DateTime.Now;
                file.CreateBy = article.CreateBy;
                file.ModifyTime = DateTime.Now;
                file.ModifyBy = article.CreateBy;
                InterfaceHelper.AddFileTalbe(file);
                article.ImageID = file.ID;
            }

            if (InterfaceHelper.AddArticleExtend(article) > 0)
            {
                result.code = "200";
                result.msg = "保存成功！";
            }
            else
            {
                result.code = "201";
                result.msg = "保存失败！";
            }
            return result;
        }
        #endregion

        #region 获取支部ID
        [HttpPost]
        public ReturnResult GetBranchID(dynamic obj)
        {
            string branchID = Convert.ToString(obj.banchId);
            string returnBranchID = InterfaceHelper.GetBranch(branchID);

            return new ReturnResult()
            {
                code = "200",
                result = returnBranchID,
                msg = "success"
            };
        }
        #endregion
    }
}
